package finance

class HistogramController {
	def historicalQueryService
	def frequenceService
	def subtractService
	def lnService
	
	def calcFrequence(vals, fieldVar) {
		def frequence = frequenceService.frequence(vals)
			
		if(params.fieldVar == 'Volume') {
			frequence = frequenceService.reduceKeys(frequence)
		}
		
		return frequence
	}
	
	def calcDeltaFrequence(vals) {
		def subtracted = subtractService.minPrev(vals)
		return frequenceService.frequence(subtracted)
	}
	
	def calcLnFrequency(vals) {
		def lned = lnService.ln(vals)
		return frequenceService.frequence(lned)
	}
	
	def calcDeltaLnFrequency(vals) {
		def deltaLned = lnService.deltaLn(vals)
		return frequenceService.frequence(deltaLned)
	}
	
	def makeResponse(vals) {
		def frequence = calcFrequence(vals, params.fieldVar)
			
		def deltasFrequency = calcDeltaFrequence(vals)
			
		def lnFrequency = calcLnFrequency(vals)
		
		def deltaLnFrequency = calcDeltaLnFrequency(vals)
			
		return [data : frequence, 
			deltaData : deltasFrequency,
			lnData : lnFrequency,
			deltaLnData : deltaLnFrequency]
	}
	
    def index = { 
		if(params.fieldVar != null) {
			def vals = historicalQueryService.queryVals( params.select1, params.fieldVar);
			return makeResponse(vals)
		}
	}
	
    def pairs = { 
		if(params.fieldVar != null) {
			def vals = historicalQueryService.queryPairVals( 
				params.select1, params.select2, params.fieldVar);
			return makeResponse(vals)
		}
	}
}
